package com.joplus.housekeeper.ui.view.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.joplus.housekeeper.R;

/**
 * author： ccm
 * time： 2017/3/15.
 * description：相机相册选择弹窗
 */

public class CameraDialog extends PopupWindow {
    private Context mContext;
    private final TextView tv_camera;
    private final TextView tv_gallery;
    private CameraType mType;
    public static int DIALOG_CAMREA = 0;
    public static int DIALOG_GALLERY = 1;

    public CameraDialog(Context context, CameraType type) {
        super(context);
        this.mType = type;
        mContext = context;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View mMenuView = inflater.inflate(R.layout.dialog_camera, null);
        tv_camera = (TextView) mMenuView.findViewById(R.id.tv_camera);
        tv_gallery = (TextView) mMenuView.findViewById(R.id.tv_gallery);


        //设置按钮监听

        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(Color.TRANSPARENT);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框

        mMenuView.findViewById(R.id.tv_dismiss).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();


            }
        });
        tv_camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mType.getType(DIALOG_CAMREA);
                dismiss();
            }
        });
        tv_gallery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mType.getType(DIALOG_GALLERY);
                dismiss();
            }
        });

    }

    private Activity mActivity;

    public void show(Activity a) {
        mActivity = a;
        backgroundAlpha(mActivity, 0.5f);
        showAtLocation(mActivity.getWindow().getDecorView(), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                if (mActivity != null) {
                    backgroundAlpha(mActivity, 1);
                }
            }
        });

    }

    public static void backgroundAlpha(Activity a, float bgAlpha) {
        WindowManager.LayoutParams lp = a.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        if (bgAlpha == 1) {
            a.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        } else {
            a.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        }
        a.getWindow().setAttributes(lp);
    }

    public interface CameraType {
        void getType(int type);
    }

}
